package com.gismore.jsonp.config;

import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import lombok.extern.log4j.Log4j2;

@Log4j2
public class AppModule extends AbstractModule{
	private static AppModule appModule = null;
	private static Injector injector = null;

	public AppModule() {
	}

	@Override
	protected void configure() {
		final Binder binder = binder();

		binder.bind(AppConfig.class).toInstance(AppConfig.me());

		//TODO: bind interface
		/*binder.bind(ItemService.class).to(ItemServiceImpl.class).in(SINGLETON);
		binder.bind(OrderService.class).to(OrderServiceImpl.class).in(SINGLETON);*/
		//TODO: bind self class(without interface or base class)
		//binder.bind(PriceService.class).in(Scopes.SINGLETON);


		//TODO: bind instance not class.
		//binder.bind(RuntimeService.class).toInstance(new RuntimeService());


		//TODO: bind named instance;
		//binder.bind(NamedService.class).annotatedWith(Names.named("impl1")).to(NamedServiceImpl1.class);
		//binder.bind(NamedService.class).annotatedWith(Names.named("impl2")).to(NamedServiceImpl2.class);
	}

	public static <T> T inject(Class<T> clazz){
		if(appModule == null){
			synchronized (AppModule.class){
				if(appModule == null){
					appModule = new AppModule();
				}
			}
		}
		if(injector == null){
			synchronized (AppModule.class){
				if(injector == null){
					injector = Guice.createInjector(appModule);
				}
			}
		}
		return injector.getInstance(clazz);
	}
}
